package com.whale.controllers.manage;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.whale.constants.MenuGroupConstants;
import com.whale.controllers.AbstractController;
import com.whale.kernel.model.tree.HtmlMenuNode;
import com.whale.web.json.tree.MenuTreeJsonProcessor;

@Controller
@RequestMapping(value = "/manage/pages")
public class ManagePagesController extends AbstractController {

	private Logger logger = LoggerFactory
			.getLogger(ManagePagesController.class);

	@RequestMapping(method = RequestMethod.GET)
	public String getManagePages(HttpServletRequest request, Model model) {
		populateNavigation(model, "/manage/pages",
				MenuGroupConstants.MANAGE_MENU_GROUP_ID);
		try {
			MenuTreeJsonProcessor menuTreeJsonProcessor = new MenuTreeJsonProcessor(
					htmlPageService, htmlMenuService);
			menuTreeJsonProcessor.populateTreeMenu(model);
		} catch (JSONException e) {
			logger.error(e.getMessage(), e);
		}
		return "manage/pages";
	}

	@RequestMapping(value = "feedChildMenus", method = RequestMethod.GET)
	public void feedChildMenus(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		String resultJSON;
		try {
			String nodeKey = ServletRequestUtils.getStringParameter(request,
					"nodeKey");
			Long nodeId = HtmlMenuNode.nodeKeytoId(nodeKey);
			MenuTreeJsonProcessor menuTreeJsonProcessor = new MenuTreeJsonProcessor(
					htmlPageService, htmlMenuService);
			JSONObject json = menuTreeJsonProcessor.feedChildMenus(nodeId);
			resultJSON = json.toString();
		} catch (ServletRequestBindingException e) {
			logger.error(e.getMessage(), e);
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			return;
		} catch (JSONException e) {
			logger.error(e.getMessage(), e);
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			return;
		}

		response.setHeader("Cache-Control",
				"no-store, no-cache, must-revalidate, max-age=0");
		response.setDateHeader("Expires", (new Date()).getTime());
		response.setContentType("application/json");

		PrintWriter pw = response.getWriter();
		try {
			logger.info("resultJSON: " + resultJSON);
			pw.write(resultJSON);
		} finally {
			pw.close();
		}
	}
	
	@RequestMapping(value = "feedTreeScopeMenus", method = RequestMethod.GET)
	public void feedTreeScopeMenus(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		String resultJSON;
		try {
			String nodeKey = ServletRequestUtils.getStringParameter(request,
					"nodeKey");
			Long nodeId = HtmlMenuNode.nodeKeytoId(nodeKey);
			MenuTreeJsonProcessor menuTreeJsonProcessor = new MenuTreeJsonProcessor(
					htmlPageService, htmlMenuService);
			JSONObject json = menuTreeJsonProcessor.feedTreeScopeMenus(nodeId);
			resultJSON = json.toString();
		} catch (ServletRequestBindingException e) {
			logger.error(e.getMessage(), e);
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			return;
		} catch (JSONException e) {
			logger.error(e.getMessage(), e);
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			return;
		}

		response.setHeader("Cache-Control",
				"no-store, no-cache, must-revalidate, max-age=0");
		response.setDateHeader("Expires", (new Date()).getTime());
		response.setContentType("application/json");

		PrintWriter pw = response.getWriter();
		try {
			logger.info("resultJSON: " + resultJSON);
			pw.write(resultJSON);
		} finally {
			pw.close();
		}
	}
}
